/**
* Copyright (c) By zengqh.
*
* This program is just for fun or demo, in the hope that it  
* will be useful, you can redistribute it and/or modify freely.
*
* Time: 2013/05/06
* File: OGLProgram.cpp
* Blog: http://www.cnblogs.com/zengqh/
**/

#include "OGLProgram.h"
#include "OGL.h"

namespace HY
{

OGLProgram::OGLProgram()
: vs_(0)
, ps_(0)
, handle_(0)
{

}

OGLProgram::~OGLProgram()
{
	release();
}

void OGLProgram::release()
{
	if (handle_)
	{
		glDeleteProgram(handle_);
	}

	HY_SAFE_DELETE(vs_);
	HY_SAFE_DELETE(ps_);
}

void OGLProgram::bind()
{
	glUseProgram(handle_);
}

void OGLProgram::unbind()
{
	glUseProgram(0);
}

VertexShader* OGLProgram::getVertexShader()
{
	return vs_;
}

bool OGLProgram::setVertexShader(VertexShader* vs)
{
	vs_ = static_cast<OGLVertexShader*>(vs);
	return true;
}

PixelShader* OGLProgram::getPixelShader()
{
	return ps_;
}

bool OGLProgram::setPixelShader(PixelShader* ps)
{
	ps_ = static_cast<OGLPixelShader*>(ps);
	return true;
}

ProgramAttribute* OGLProgram::getAttribute(const std::string& name)
{
	std::map<std::string, ProgramAttribute*>::iterator iter = attribute_list_.find(name);

	if (iter != attribute_list_.end())
	{
		return iter->second;
	}

	return 0;
}

ProgramUniform* OGLProgram::getUniform(const std::string& name)
{
	std::map<std::string, ProgramUniform*>::iterator iter = uniform_list_.find(name);

	if (iter != uniform_list_.end())
	{
		return iter->second;
	}

	return 0;
}

void OGLProgram::addUniform(const std::string& name, ProgramUniform* u)
{
	if (u && !name.empty())
	{
		uniform_list_[name] = u;
	}
}

void OGLProgram::addAttribute(const std::string& name, ProgramAttribute* a)
{
	if (a && !name.empty())
	{
		attribute_list_[name] = a;
	}
}

}